package com.kfm.base.array2;

import java.util.Arrays;

public class Inertion {

    public static void main(String[] args) {
        /*
        插入排序
         */
        int[] arr = {2, 3, 1, 8, 3, 9, 6};

        for (int i = 1; i < arr.length; i++) {
            int current = arr[i]; // 当前要排序的数
            int preIndex = i - 1; // 前一个元素的下标

            while (preIndex >= 0 && arr[preIndex] > current){
                arr[preIndex + 1] = arr[preIndex]; // 后移
                preIndex --;
            }

            arr[preIndex + 1] = current;
        }

        System.out.println(Arrays.toString(arr));

    }
}
